/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.ctu.guiproject.server;

import cz.ctu.guiproject.server.dispatcher.Dispatcher;
import cz.ctu.guiproject.server.manager.LayoutManager;
import cz.ctu.guiproject.server.manager.LayoutManagerImpl;
import cz.ctu.guiproject.server.shared.Event;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author tomas.buk
 */
public class UIServerString implements UIServer {

    private final List<EventObserver> observers;
    private Event currentEvent;
    private final Dispatcher dispatcher;
    private final LayoutManager layoutManager;

    @SuppressWarnings("LeakingThisInConstructor")
    public UIServerString(int port) {
        observers = new LinkedList<>();
        dispatcher = Dispatcher.getInstance();
        dispatcher.init(port, this);
        layoutManager = LayoutManagerImpl.getInstance();
    }

    @Override
    public void eventOccured(Event event) {
        currentEvent = event;
        notifyObservers();
    }

    @Override
    public void registerObserver(EventObserver o) {
        if (!observers.contains(o)) {
            observers.add(o);
        }
    }

    @Override
    public void removeObserver(EventObserver o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (EventObserver o : observers) {
            o.update(currentEvent);
        }
    }

    @Override
    public LayoutManager getLayoutManager() {
        return layoutManager;
    }
}
